package cn.hg.solon.youcan.framework.web.admin.interceptor;

import java.util.Date;

import org.dromara.hutool.core.date.DateUtil;
import org.noear.solon.core.aspect.Interceptor;
import org.noear.solon.core.aspect.Invocation;
import org.noear.solon.core.handle.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hg.solon.youcan.framework.web.admin.annotation.Logging;
import cn.hg.solon.youcan.framework.web.admin.filter.OperateLogReporter;

/**
 * @author 胡高
 * @date 2023/08/03
 */
public class LoggingInterceptor implements Interceptor {

    protected Logger log = LoggerFactory.getLogger(this.getClass());

    /* (non-Javadoc)
     * @see org.noear.solon.core.aspect.Interceptor#doIntercept(org.noear.solon.core.aspect.Invocation)
     */
    @Override
    public Object doIntercept(Invocation inv) throws Throwable {
        this.log.debug("监听到操作日志：");

        Date start = DateUtil.now();

        try {
            return inv.invoke();
        } catch (Throwable e) {
            throw e;
        } finally {
            // 对有Log注解的操作记录其操作日志
            OperateLogReporter.logging(Context.current(), inv.method().getAnnotation(Logging.class), start, DateUtil.now());
        }
    }

}
